/**
 *
 *用于缓存的Ajax请求的Ajax.js
 *Daniel
 *2017.12.19
 */
define(function () {
    var ajax = function (){
        this.get = XhrG;
        this.post = XhrP;
        this.json = XhrJ;
    };
    
    function XhrG(objectData, callback, success){
        var xhr = new XMLHttpRequest();
        var path = objectData.url;
        if (success !== undefined)
        {
            xhr.onreadystatechange = success;
        } else
        {
            xhr.onreadystatechange = function ()
            {
                if (xhr.readyState == 4)
                {
                    if (xhr.status == 200)
                    {
                        var data = xhr.responseText;
                        objectData.data = data;
                        callback(objectData);
                    }
                }
            };
        }
        xhr.open("GET", path, true);
        // xhr.setRequestHeader("Cache-Control","max-age = 1209600");
        xhr.setRequestHeader("Cache-Control","max-age = 0");
        xhr.setRequestHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.");
        xhr.send(null);
    }
    
    function XhrP(objectData, callback, data, success){
        var xhr;
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xhr=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
            xhr=new ActiveXObject("Microsoft.XMLHTTP");
        }
        var path = objectData.url;
        if (success !== undefined) {
            xhr.onreadystatechange = success;
        } else {
            xhr.onreadystatechange = function ()
            {
                if (xhr.readyState==4 && xhr.status==200)
                {
                    var data = xhr.responseText;
                    objectData.data = data;
                    callback(objectData);
                }
                
            };
        }
        xhr.open("POST", path, true);
        // xhr.setRequestHeader("Cache-Control","max-age = 1209600");
        xhr.setRequestHeader("Cache-Control","max-age = 0");
        xhr.setRequestHeader("Content-type","application/json;charset=utf-8");
        xhr.send(data);
    }
    
    function XhrJ(objectData, callback,success){
        var path = objectData.url;
        var com = document.createElement("script");
        var dom = document.createElement("script");
        var fun = success.toString();
        var texts = JSON.stringify(objectData);
        dom.id = "jsonp"+objectData.id;
        com.id = "jsonpC"+objectData.id;
        dom.src = path+""+objectData.id;
        com.innerHTML ="var "+callback+""+objectData.id+" = function(data){" +
            "var text = " +texts+";" +
            "text.data = data;"+
            "var call = "+fun+";"+
            "if(call!==undefined)call(text);" +
            "var dom = document.getElementById('"+dom.id+"');" +
            "var com = document.getElementById('"+com.id+"');" +
            "var head = document.head;" +
            "head.removeChild(dom);" +
            "head.removeChild(com)" +
            "}" ;
        var head = document.head;
        head.appendChild(com);
        head.appendChild(dom);
    }
    
    return new ajax();
});